using AspectCore.Configuration;
using AspectCore.Extensions.DependencyInjection;
using AspectCore.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web;
using Serilog;
using SN.WebApi.AOP;
using SN.WebApi.Services;
using SqlSugar;

// Early init of NLog to allow startup and exception logging, before host is built
//var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
//logger.Debug("init main");


Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.File("serilog")
    .CreateLogger();

var builder = WebApplication.CreateBuilder(args);

//builder.Logging.ClearProviders();
//builder.Logging.AddLog4Net("cfg/log4Net.config");
//builder.Logging.AddNLog("cfg/nlog.config");//.Services();

builder.Logging.AddSerilog();// .UseSerilog();


builder.Services.AddControllers();
//.AddLog4Net();//AddLog4Net("CfgFile/log4Net.config");
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// AOP
builder.Services.AddTransient<IUserService, UserService>();
builder.Services.ConfigureDynamicProxy(config => {
    config.Interceptors.AddTyped<MemercacheFilter>();
});
builder.Services.AddMemoryCache();
builder.Host.UseDynamicProxy();

//注册上下文：AOP里面可以获取IOC对象，如果有现成框架比如Furion可以不写这一行
builder.Services.AddHttpContextAccessor();
//注册SqlSugar
builder.Services.AddSingleton<ISqlSugarClient>(s =>
{
    SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
    {
        DbType = SqlSugar.DbType.MySql,
        ConnectionString = "Server=127.0.0.1;Port=3306;Database=DBTest;Uid=root;Pwd=ybdai930728;",
        IsAutoCloseConnection = true,
    },
   db =>
   {         
       db.Aop.OnLogExecuting = (sql, pars) =>
       {
            Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
       };
   });
    return sqlSugar;
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapControllers();
app.Run();
 